﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.SmallPic;
using MINERP.BLL.Common;
using MINERP.DAL;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using System.Collections;

namespace MINERP.BLL.OldSmallPic
{
    public class OldSamllPicMainBLL
    {
        public List<ProductView> GetCurTaskList(int curTechid)//根据节点ID得到该岗位的任务列表
        {
            List<ProductView> listResult = new List<ProductView>();

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int orderDoing = (int)OrderInfoStatus.Doing;
                foreach (var WFL in db.WorkOrderFlowList.Where(wf => wf.tb_Tech.TechId == curTechid && wf.Status == orderDoing).ToList())
                {
                    #region load data
                    if (!WFL.orderInfoReference.IsLoaded)
                    {
                        WFL.orderInfoReference.Load();
                    }

                    if (WFL.orderInfo.status != (int)OrderInfoStatus.Doing)
                    {
                        continue;
                    }

                    if (!WFL.orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        WFL.orderInfo.Tb_OrderBagReference.Load();
                    }
                    if (!WFL.orderInfo.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        WFL.orderInfo.Tb_OrderBag.ShopReference.Load();
                    }
                    if (!WFL.orderInfo.photoInfo.IsLoaded)
                    {
                        WFL.orderInfo.photoInfo.Load();
                    }
                    #endregion

                    ProductView view = new ProductView()
                    {

                        CtmName = WFL.orderInfo.CtmName,
                        IsSelect = false,
                        OrderCode = WFL.orderInfo.OrderProductCode,
                        OrderName = WFL.orderInfo.OrderProductName,
                        OrderPath = WFL.orderInfo.OrilOrderPath,
                        OrderProductId = WFL.orderInfo.OrderProductId,

                        OrderTime = WFL.orderInfo.Tb_OrderBag.OrderTime,
                        Priority = WFL.orderInfo.Priority == null ? 0 : (int)WFL.orderInfo.Priority,
                        Remark = WFL.orderInfo.Remark,
                        ShopName = WFL.orderInfo.Tb_OrderBag.Shop.shopName,
                        OrderNum = WFL.orderInfo.productNum
                    };

                    view.Photos = new List<PhotoInfo_Info>();


                    foreach (var pi in WFL.orderInfo.photoInfo.ToList())
                    {
                        //if (pi.status == (int)PhotoStatus.Printed)//已打印过的就不加入到此列表
                        //{
                        //    continue;
                        //}
                        if (string.IsNullOrEmpty(view.PaperFaceName))
                        {
                            if (!pi.paperFaceReference.IsLoaded)
                            {
                                pi.paperFaceReference.Load();
                            }
                            if (!pi.paperTypeReference.IsLoaded)
                            {
                                pi.paperTypeReference.Load();
                            }

                            view.PaperFaceName = pi.paperFace.Name;
                            view.PaperTypeName = pi.paperType.Name;
                        }

                        PhotoInfo_Info pinfo = new PhotoInfo_Info()
                        {
                            PicPath = pi.picPath,
                            TotalNum = pi.TotalNum == null ? 1 : (int)pi.TotalNum,
                            Remark = pi.Remark
                        };
                        view.Photos.Add(pinfo);
                    }
                    if (view.Photos.Count != 0)
                    {
                        listResult.Add(view);
                    }
                }
            }

            return listResult;
        }

        public string GetReportDir()//得到扩印模块的照片目录
        {
            string strPath = "";
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                FileDirPath ft = db.FileDirPath.FirstOrDefault(f => f.id == 7);
                if (ft != null)
                {
                    strPath = ft.FileDir;
                }

            }
            return strPath;
        }

        public bool CompleteTask(ProductView oiTask, int curTechId, int userId)//完成
        {
            bool result = true;
            WorkFlowListBLL wflBLL = new WorkFlowListBLL();

            result = wflBLL.OverCurTech(curTechId, userId, oiTask.OrderProductId);
            if (!result)
            {
                return result;
            }

            //在photoinfo表中复制记录
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                orderInfo oi = db.orderInfo.FirstOrDefault(o => o.OrderProductId == oiTask.OrderProductId);
                if (oi != null)
                {
                    if (!oi.photoInfo.IsLoaded)
                    {
                        oi.photoInfo.Load();
                    }
                    //ArrayList listAdd = new ArrayList();
                    foreach (var pi in oi.photoInfo.ToList())
                    {
                        if (pi.status == (int)PhotoStatus.Printed)
                        {
                            continue;
                        }

                        int totalNum = oi.productNum * (pi.TotalNum == null ? 1 : (int)pi.TotalNum);
                        if (!pi.paperFaceReference.IsLoaded)
                        {
                            pi.paperFaceReference.Load();
                        }
                        if (!pi.paperTypeReference.IsLoaded)
                        {
                            pi.paperTypeReference.Load();
                        }
                        pi.TotalNum = 1;
                        pi.status = (int)PhotoStatus.Printed;
                        if (totalNum > 1)
                        {
                            for (int i = 0; i < totalNum - 1; i++)
                            {
                                db.AddTophotoInfo(new photoInfo()
                                {
                                    Height = pi.Height,
                                    IsModifySize = pi.IsModifySize,
                                    MakeUpNum = pi.MakeUpNum,
                                    OldPicPath = pi.OldPicPath,
                                    orderInfo = pi.orderInfo,
                                    paperFace = pi.paperFace,
                                    paperType = pi.paperType,
                                    PhotoBarCode = "00",
                                    photoName = pi.photoName,
                                    picPath = pi.picPath,
                                    pixHeight = pi.pixHeight,
                                    pixWidth = pi.pixWidth,
                                    Remark = pi.Remark,
                                    status = pi.status,
                                    status2 = pi.status2,
                                    TotalNum = 1,
                                    Width = pi.Width,
                                    printerId = pi.printerId,
                                    userId = pi.userId
                                });
                            }
                        }
                    }

                }
                try
                {
                    db.SaveChanges();
                    //更改被复制的条码
                    if (!oi.photoInfo.IsLoaded)
                    {
                        oi.photoInfo.Load();
                    }

                    foreach (var pi in oi.photoInfo.ToList())
                    {
                        pi.PhotoBarCode = "3" + pi.photoId.ToString().PadLeft(9, '0');
                    }

                    db.SaveChanges();

                }
                catch (Exception)
                {
                    result = false;
                }

            }

            return result;
        }

        public bool SetExportDir(string p)
        {
            bool result = true;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                FileDirPath f = db.FileDirPath.FirstOrDefault(ft => ft.id == 7);
                if (f != null)
                {
                    f.FileDir = p;
                    result = true;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {

                        result = false;
                    }
                }
                else
                {
                    result = false;
                }
            }

            return result;
        }

        public ProductView GetProductViewByCode(string strCode)
        {
            ProductView view = null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                orderInfo oi = db.orderInfo.FirstOrDefault(o => o.OrderProductCode == strCode);
                if (oi != null)
                {
                    #region loadData
                    if (!oi.Tb_OrderBagReference.IsLoaded)
                    {
                        oi.Tb_OrderBagReference.Load();
                    }
                    if (!oi.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        oi.Tb_OrderBag.ShopReference.Load();
                    }
                    #endregion

                    if (oi != null)
                    {
                        view = new ProductView()
                        {

                            CtmName = oi.CtmName,
                            OrderCode = oi.OrderProductCode,
                            OrderName = oi.OrderProductName,
                            OrderProductId = oi.OrderProductId,
                            Remark = oi.Remark,
                            ShopName = oi.Tb_OrderBag.Shop.shopName,
                            OrderNum = oi.productNum
                        };
                        view.Photos = new List<PhotoInfo_Info>();
                        if (!oi.photoInfo.IsLoaded)
                        {
                            oi.photoInfo.Load();
                        }

                        foreach (var pi in oi.photoInfo.ToList())
                        {
                            view.Photos.Add(new PhotoInfo_Info() { TotalNum = (pi.TotalNum == null ? 1 : (int)pi.TotalNum), PhotoId = pi.photoId });
                        }

                        var pit = oi.photoInfo.FirstOrDefault();
                        if (pit != null)
                        {
                            if (!pit.paperTypeReference.IsLoaded)
                            {
                                pit.paperTypeReference.Load();
                            }
                            if (!pit.paperFaceReference.IsLoaded)
                            {
                                pit.paperFaceReference.Load();
                            }

                            view.PaperTypeName = pit.paperType.Name;
                            view.PaperFaceName = pit.paperFace.Name;
                        }
                    }
                }
            }
            return view;
        }


        public Printer_Info GetPrinterObj(int id)
        {
            Printer_Info printerInfo = new Printer_Info();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                printer printerobj = enty.printer.FirstOrDefault(p => p.PrinterID == id);
                printerInfo.HotDirPath = printerobj.hotDirPath;
                printerInfo.ICCPath = printerobj.ICCPath;
                printerInfo.IsTransverse = (bool)printerobj.IsTransverse;
                printerInfo.MonitorDirPath = printerobj.MonitorDirPath;
                printerInfo.PDes = printerobj.PDes;
                printerInfo.PDPI = printerobj.PDPI;
                printerInfo.PLength = printerobj.PLength;
                printerInfo.PName = printerobj.PName;
                printerInfo.PrinterID = printerobj.PrinterID;
                printerInfo.PType = printerobj.PType;
                printerInfo.PWidth = printerobj.PWidth;
                printerInfo.Status = printerobj.status;
                printerInfo.Status2 = (int)printerobj.status2;
                //printerInfo.GridNum = printerobj.GridNum == null ? 0 : (int)printerobj.GridNum;
            }
            return printerInfo;
        }

        /// <summary>
        /// 得到orderInfo
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <returns></returns>
        public MINERP.MODEL.SmallPic.OrderInfo_Info GetOrderInfoToPrintTicket(int orderProductId, int printerId)
        {
            MINERP.MODEL.SmallPic.OrderInfo_Info oi = new MINERP.MODEL.SmallPic.OrderInfo_Info();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                MINERP.MODEL.SmallPic.OrderInfo_Info orderInfo_info = new MINERP.MODEL.SmallPic.OrderInfo_Info();
                List<PhotoInfo_Info> listPhoto = new List<PhotoInfo_Info>();

                orderInfo orderinfo = enty.orderInfo.FirstOrDefault(o => o.OrderProductId == orderProductId);
                if (!orderinfo.photoInfo.IsLoaded)
                {
                    orderinfo.photoInfo.Load();
                }
                int photoNum = 0;
                int totalPhotoNum = 0;
                foreach (var item in orderinfo.photoInfo)
                {
                    if (item.printerId == printerId)
                    {
                        for (int i = 0; i < item.TotalNum; i++)
                        {
                            PhotoInfo_Info photoInfo_info = new PhotoInfo_Info();
                            photoInfo_info.Height = item.Height;
                            photoInfo_info.Width = item.Width;
                            listPhoto.Add(photoInfo_info);
                        }
                        photoNum += (int)item.TotalNum;
                    }
                    totalPhotoNum += (int)item.TotalNum;
                }
                oi.OrderProductId = orderinfo.OrderProductId;
                oi.OrderProductName = orderinfo.OrderProductName;
                oi.CtmName = orderinfo.CtmName;
                oi.Priority = (int)orderinfo.Priority;
                oi.Remark = orderinfo.Remark;
                oi.PhotoInfoList = listPhoto;
                oi.PhotoNumAndTotalNum = photoNum.ToString() + "/" + totalPhotoNum.ToString();
            }
            return oi;
        }
    }
}
